package com.hhs.storage.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;


/**
 * 得到下级或上级的uuid
 * @author Administrator
 *
 */
public class TreeUtil {
	/**
	 * 得到实例方法
	 * @return
	 */
	public static TreeUtil getInstance(){
		return new TreeUtil();
	}
	/**
	 * 节点
	 */
	public static class Node {
	    private String id;  
	    private String name;  
	    private String pid;
	    
	    public Node(String id, String pid,String name) {
			this.id = id;
			this.name = name;
			this.pid = pid;
		}    
		public Node() {
		}
		public String getId() {  
	        return id;  
	    }  
	    public void setId(String id) {  
	        this.id = id;  
	    }  
	    public String getName() {  
	        return name;  
	    }  
	    public void setName(String name) {  
	        this.name = name;  
	    }  
	    public String getPid() {  
	        return pid;  
	    }  
	    public void setPid(String pid) {  
	        this.pid = pid;  
	    }  
	}
	/**
	 * 子节点查找类
	 * Created by tl on 2016/4/10. 
	 */  
	public  class NodeRecursion {
		//子节点 list 
	    private    List<Node> parentNode=Collections.synchronizedList(new ArrayList<Node>());
	    //子节点 list 
	    private    List<Node> childNode=Collections.synchronizedList(new ArrayList<Node>());  
	    /** 
	     * 获取某个父节点下面的所有子节点 
	     * @param nodeList  所有节点
	     * @param pid 
	     * @return 
	     */  
	    public  List<Node> treeNodeList( List<Node> nodeList, String pid){
	        for(Node mu: nodeList){  
	            //遍历出父id等于参数的id，add进子节点集合  
	            if(StringUtil.zNull(pid).equals(mu.getPid())){
	                //递归遍历下一级  
	                treeNodeList(nodeList,mu.getId());
	                childNode.add(mu);  
	            }  
	        }  
	    return childNode;  
	    }
		/**
		 * 获取某个父节点下面的n层子节点
		 * @param nodeList  所有节点
		 * @param pid
		 * @return
		 */
		public  List<Node> treeNodeList( List<Node> nodeList, String pid,int n){
			if(n==0){
				return childNode;
			}
			n--;
			for(Node mu: nodeList){
				//遍历出父id等于参数的id，add进子节点集合
				if(StringUtil.zNull(pid).equals(mu.getPid())){
					//递归遍历下一级
					treeNodeList(nodeList,mu.getId(),n);
					childNode.add(mu);
				}
			}
			return childNode;
		}
	    /** 
	     * 获取某个节点上级所有
	     * @param nodeList  所有节点
	     * @param sid
	     * @return 
	     */  
	    public  List<Node> treeNodeListParent( List<Node> nodeList, int sid){  
	        for(Node mu: nodeList){
	            if(Integer.valueOf(StringUtil.tozZero(mu.getId()))==sid){
	                //递归遍历下一级  
	            	treeNodeListParent(nodeList,Integer.valueOf(mu.getPid()));  
	                parentNode.add(mu);  
	            }  
	        }  
	    return parentNode;  
	    }

	}

	public static void main(String[] args) {
		List<Node> list=new ArrayList<>();
		list.add(new TreeUtil.Node("1","0","yj"));
		list.add(new TreeUtil.Node("2","0","yj"));
		list.add(new TreeUtil.Node("3","0","yj"));
		NodeRecursion treeutil = TreeUtil.getInstance().new NodeRecursion();
		List<Node> nodes = treeutil.treeNodeList(list, "0");
		System.out.println(nodes);
	}
}
